
<template>
  <el-container>
    <el-header>
      <div>
        <i class="iconfont icon-applications" /> 菜单管理
        <el-button
          class="filter-item"
          type="primary"
          size="small"
          style="margin-left:10px"
          icon="el-icon-plus"
          @click="handleCreate"
        >新增</el-button>
      </div>
    </el-header>
    <el-main class="bg-white main-full-height">
      <el-table
        v-loading="dataLoading"
        :data="dataTable"
        row-key="Id"
        border
        fit
        highlight-current-row
        style="width: 100%;margin-top:10px;"
        :tree-props="{children: 'Children'}"
      >
        <el-table-column label="菜单名称" width="160px" prop="MenuName" />
        <el-table-column label="图标" prop="Icon" width="50px">
          <template slot-scope="{row}">
            <i class="iconfont" :class="row.Icon" />
          </template>
        </el-table-column>
        <el-table-column label="路由地址" align="center" prop="Url" />
        <el-table-column label="页面地址" align="center" prop="ViewUrl" />
        <el-table-column label="排序" align="center" width="50px" prop="Sort" />
        <el-table-column label="是否显示" align="center" width="100px" prop="IsShow">
          <template slot-scope="{row}">
            <el-tag v-if="row.IsShow" effect="dark" type="success">显示</el-tag>
            <el-tag v-else effect="dark" type="info">隐藏</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="270" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button
              type="text"
              size="mini"
              @click="handleUpdate(row)"
            >修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      class="add-department"
    >
      <el-form
        ref="dataForm"
        :rules="dataRules"
        :model="temp"
        label-width="120px"
        style="width: 100%; "
      >
        <el-form-item
          label="上级菜单"
          prop="ParentId"
          class="child"
        >
          <el-select v-model="temp.ParentId" placeholder="">
            <el-option label="顶级菜单" value="0" />
            <el-option v-for="m in TopMenus" :key="m.Value" :label="m.Label" :value="m.Value" />
          </el-select>
        </el-form-item>
        <el-form-item
          label="菜单名称"
          prop="MenuName"
          class="child"
        >
          <el-input v-model="temp.MenuName" />
        </el-form-item>
        <el-form-item
          label="路由地址"
          prop="Url"
          class="child"
        >
          <el-input v-model="temp.Url" />
        </el-form-item>
        <el-form-item
          label="页面地址"
          prop="ViewUrl"
          class="child"
        >
          <el-input v-model="temp.ViewUrl" />
        </el-form-item>
        <el-form-item
          label="排序号"
          prop="Sort"
          class="child"
        >
          <el-input v-model="temp.Sort" />
        </el-form-item>
        <el-form-item
          label="图标"
          prop="Icon"
          class="child"
        >
          <el-input v-model="temp.Icon" @focus="showIcon"><i slot="prefix" class="iconfont" style="font-size: 24px;line-height: 40px;" :class="temp.Icon" /></el-input>
        </el-form-item>
        <el-form-item
          label="是否显示"
          prop="IsShow"
          class="child"
        >
          <el-radio-group v-model="temp.IsShow">
            <el-radio
              :label="true"
              checked
            >显示</el-radio>
            <el-radio :label="false">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="备注"
          class="child"
          prop="Remark"
        >
          <el-input v-model="temp.Remark" type="textarea" :autosize="{ minRows: 2}" />
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">关闭</el-button>
        <el-button
          type="primary"
          @click="dialogStatus==='create'?createData():updateData()"
        >保存</el-button>
      </div>
    </el-dialog>
    <icon ref="iconref" @checked="checked" />
  </el-container>
</template>
<script>
import icon from '@/views/components/common/icon'
import { getMenusTree, editMenu, getTopMenus } from '@/api/menu'
export default {
  components: {
    icon
  },
  data() {
    return {
      TopMenus: [],
      dialogFormVisible: false,
      dialogStatus: '',
      temp: {
        Id: '',
        MenuName: '',
        ParentId: '',
        Url: '',
        ViewUrl: '',
        Icon: '',
        IsShow: true,
        Sort: 1,
        Remark: ''
      },
      textMap: {
        update: '编辑',
        create: '新增'
      },
      tableKey: 0,
      dataTable: [],
      total: 0,
      dataLoading: false,
      dataRules: {
        MenuName: [
          { required: true, message: '请填写菜单名称', trigger: 'blur' },
          { max: 10, message: '菜单名称不能超过10个字', trigger: 'blur' }
        ],
        ParentId: [
          { required: true, message: '请选择所属菜单', trigger: 'blur' }
        ],
        Url: [
          { required: true, message: '请填写路由地址', trigger: 'blur' }
        ],
        ViewUrl: [
          { required: true, message: '请填写页面地址', trigger: 'blur' }
        ],
        Icon: [
          { required: true, message: '请选择应用图标', trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    this.getTopMenus()
    this.getData()
  },
  methods: {

    getData() {
      this.dataLoading = true
      getMenusTree().then(res => {
        this.dataTable = res.Data
        this.total = res.TotalItemCount
        this.dataLoading = false
      }).catch(() => {
        this.dataLoading = false
      })
    },
    getTopMenus() {
      getTopMenus().then(res => {
        this.TopMenus = res.Data
      })
    },
    resetTemp() {
      this.temp = {
        Id: '',
        MenuName: '',
        ParentId: '',
        Url: '',
        ViewUrl: '',
        Icon: '',
        IsShow: true,
        Sort: 1,
        Remark: ''
      }
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          editMenu(this.temp).then(() => {
            this.dialogFormVisible = false
            this.getData()
          })
        }
      })
    },
    handleUpdate(row) {
      this.resetTemp()
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    //
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          editMenu(tempData).then(() => {
            this.dialogFormVisible = false
            this.getData()
          })
        }
      })
    },
    showIcon() {
      this.$refs.iconref.showDialog()
    },
    checked(icon) {
      this.temp.Icon = icon
    }
  }
}
</script>

<style>
.el-dialog {
  width: 800px;
}
.app-container {
  margin: 20px 0;
}
</style>
